import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

// 路由懒加载
// 配置路由
const routes = [
  {
    path: '/home',
    name: 'Home',
    component: () => import('../views/home.vue')
  },
  {
    path: '/sou',
    name: 'Sou',
    component: () => import('../views/sou.vue')
  },
  {
    path: '/fenlei',
    name: 'Fenlei',
    component: () => import('../views/fenlei.vue')
  },
  {
    path: '/fenxiao',
    name: 'Fenxiao',
    component: () => import('../views/fenxiao.vue')
  },
  {
    path: '/detail',
    name: 'Detail',
    component: () => import('../views/detail.vue')
  },
  {
    path: '/shezhi',
    name: 'Shezhi',
    component: () => import('../views/shezhi.vue')
  },
  {
    path: '/login',
    name: 'Login',
    component: () => import('../views/login.vue')
  },
  {
    path: '/telelogin',
    name: 'Telelogin',
    component: () => import('../views/telelogin.vue')
  },
  {
    path: '/register',
    name: 'Register',
    component: () => import('../views/register.vue')
  },
  {
    path: '/cart',
    name: 'Cart',
    component: () => import('../views/cart.vue')
  },
  {
    path: '/xiaoming',
    name: 'Xiaoming',
    component: () => import('../views/xiaoming.vue'),
    //? 路由独享守卫
    beforeEnter(to, from, next){
      alert('我是路由独享守卫');
      console.log(to);
      console.log(from);
      next();
    }
  },
  {
    path: '/bb',
    name: 'Bb',
    component: () => import('../views/bb.vue')
  },
  {
    path: '/cc',
    name: 'Cc',
    component: () => import('../views/cc.vue')
  },
  {
    path: '/paihang',
    name: 'Paihang',
    component: () => import('../views/paihang.vue'),
    children: [
      {
        path: 'zonghe',
        name: 'Zonghe',
        component: () => import('../views/paihang/zonghe.vue')
      },
      {
        path: 'quanhou',
        name: 'Quanhou',
        component: () => import('../views/paihang/quanhou.vue')
      },
      {
        path: 'xiaoliang',
        name: 'Xiaoliang',
        component: () => import('../views/paihang/xiaoliang.vue')
      },
      {
        path: '/paihang',
        redirect: 'zonghe',
      },
    ]
  },
  {
    path: '/wode',
    name: 'Wode',
    component: () => import('../views/wode.vue')
  },
  {
    path: '/404',
    name: '404',
    component: () => import('../views/404.vue')
  },
  {
    path: '/',
    redirect: '/home'
  },
  {
    path: '*',
    redirect: '/404',
  },
]

const router = new VueRouter({
  // mode: 'history',
  base: process.env.BASE_URL,
  routes
})


//? 全局前置守卫
router.beforeEach((to, from, next) => {
  if (to.path == '/bb') {
    alert('我是全局前置守卫');
    console.log(from);
  }
  next();
})
//? 全局后置守卫
router.beforeEach((to, from, next) => {
  if (to.path == '/bb') {
    alert('我是全局后置守卫');
    console.log(to);
    console.log(from);
  }
  next();
})






export default router
